<template>
	<view class="inwardAppoint">
		<uni-nav-bar
			color="#ffffff"
			background-color="#00aded"
			:statusBar="true"
			title="运输需求"
			fixed="true"
			left-icon="left"
			left-text="返回"
			@clickLeft="back"
			@clickRight="addAppoint"
			right-icon="plus-filled"
			right-Text="新增"
			rightPermissionAttr="ADD"
		/>
		<view class="inwardAppoint_top">
			<!-- 搜索 -->
			<!-- 搜索 -->
			<dil-search :columnData="options.columnDataList" @onSearch="onSearch" v-if="options.columnDataList.length > 0"></dil-search>
		</view>
		<view class="inwardAppoint_main">
			<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
		</view>
		<view class="inwardAppoint_floot"></view>
	</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
export default {
	data() {
		return {
			options: {
				requestUrl: '/api/v1/ams/getproductionRequirementList?apiId=656',
				// requestUrl: '/api/v1/ams/getproductionRequirementList?apiId=560',
				requestQuery: {},
				cardSlotList: [
					{
						label: '删除',
						icon: 'trash',
						emitEvent: 'delete',
						showCondition: "['待申报'].includes(item.status)"
					},
					{
						label: '申报',
						icon: 'wlIcon-shenbao',
						emitEvent: 'declare',
						showCondition: "['待申报'].includes(item.status)"
					},
					{
						label: '撤回',
						icon: 'wlIcon-withdraw',
						emitEvent: 'unDeclare',
						showCondition: "['已申报'].includes(item.status) && ['0'].includes(item.alternateFields2)"
					},
					{
						label: '复制',
						icon: 'wlIcon-fuzhi',
						emitEvent: 'copy'
					},
					{
						label: '运输详情',
						icon: 'bars',
						emitEvent: 'transOrderDetails',
						showCondition: "['已制订计划'].includes(item.status)"
					}
				],
				columnDataList: [],
				totalNum: 0
			},
			initialMap: {},
			businessTypeList: [],
			businessType: 0
		};
	},
	onShow() {
		uni.$emit('onShow', 1);
		this.getOrderSection();
	},
	async onLoad(options) {
		await this.getBusinessType();
		// this.businessType = options.businessType;
		// if (options.businessType === '0') {
		// 	//查询计重数据
		// 	this.initialMap.businessTypeList = this.businessTypeList
		// 		.filter((e) => {
		// 			return e.businessTypeName.includes('计重');
		// 		})
		// 		.map((e) => {
		// 			return e.businessTypeName;
		// 		});
		// } else {
		// 	//查询计件数据
		// 	this.initialMap.businessTypeList = this.businessTypeList
		// 		.filter((e) => {
		// 			return !e.businessTypeName.includes('计重');
		// 		})
		// 		.map((e) => {
		// 			return e.businessTypeName;
		// 		});
		// }
		// this.options.requestQuery = { ...this.initialMap };
	},
	mounted() {},
	methods: {
		async getBusinessType() {
			const res = await this.$myRequest({
				url: '/api/v1/uc/getBusinessTypeLike',
				data: {
					businessScene: '生产'
				}
			});
			this.businessTypeList = res.data.data;
		},
		getOrderSection(item) {
			console.log("item",item);
			this.options.requestQuery = JSON.parse(JSON.stringify(Object.assign(this.options.requestQuery,item)));
		},
		onSearch(item) {
			this.getOrderSection(item);
		},
		back() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		emitEvent(eventName, cardItem) {
			this[eventName](cardItem);
		},
		async copy(item) {
			const res = await this.$myRequest({
				url: '/api/v1/ams/getSerialNumber',
				data: {
					businessFlag: 'SC'
				}
			}).catch(() => {
				uni.showToast({
					duration: 2000,
					title: '获取通知单号失败',
					icon: 'success'
				});
			});
			if (res.data.status == 'succeed') {
				item.serialNumber = res.data.data.serialNumber;
				item.transRequirementId = res.data.data.transRequirementId;
				this.updateInwardAppointItem(item);
			} else {
				uni.showToast({
					duration: 2000,
					title: '获取通知单号失败',
					icon: 'success'
				});
			}
			uni.navigateTo({
				url: '/pages/transAppoint/inwardAppointAdd?navBarTitle=复制调拨通知单&type=copy'
			});
		},
		async addAppoint() {
			const res = await this.$myRequest({
				url: '/api/v1/ams/getSerialNumber',
				data: {
					businessFlag: 'SC'
				}
			}).catch(() => {
				uni.showToast({
					duration: 2000,
					title: '获取通知单号失败',
					icon: 'success'
				});
			});
			if (res.data.status == 'succeed') {
				this.updateInwardAppointItem({ ...res.data.data });
			} else {
				uni.showToast({
					duration: 2000,
					title: '获取通知单号失败',
					icon: 'success'
				});
			}
			uni.navigateTo({
				url: '/pages/transAppoint/inwardAppointAdd?navBarTitle=新增运输需求&type=add'
			});
			// if (this.businessType == '0') {
			// 	uni.navigateTo({
			// 		url: '/pages/transAppoint/inwardAppointAddWeight?navBarTitle=新增运输需求&type=add'
			// 	});
			// } else {
			// 	uni.navigateTo({
			// 		url: '/pages/transAppoint/inwardAppointAdd?navBarTitle=新增运输需求&type=add'
			// 	});
			// }
		},
		inwardPlanAdd(item) {
			this.updateInwardAppointItem(item);
			uni.navigateTo({
				url: '/pages/transPlan/inwardPlanAdd'
			});
		},
		delete(item) {
			uni.showModal({
				cancelText: '取消',
				confirmText: '确定',
				title: '提示',
				content: '是否删除该需求(仅能删除待申报的需求)',
				success: (res) => {
					if (res.confirm) {
						this.deleteInwardAppointItem(item);
					} else {
						uni.showToast({
							title: '用户取消操作',
							type: 'info',
							duration: 2000
						});
					}
				}
			});
		},
		async deleteInwardAppointItem(item) {
			let data = {
				transRequirementId: item.transRequirementId + ','
			};
			const res = await this.$myRequest({
				url: '/api/v1/ams/getProductiveRequirementDelete',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'success'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					icon: 'success',
					duration: 2000
				});
				setTimeout(() => {
					this.getOrderSection();
				}, 2200);
			} else {
				uni.showToast({
					title: res.data.message,
					icon: 'error',
					duration: 2000
				});
			}
		},
		declare(item) {
			uni.showModal({
				cancelText: '取消',
				confirmText: '确定',
				title: '提示',
				content: '是否申报该需求',
				success: (res) => {
					if (res.confirm) {
						this.declareInwardAppointItem(item);
					} else {
						uni.showToast({
							title: '用户取消操作',
							type: 'info',
							duration: 2000
						});
					}
				}
			});
		},
		async declareInwardAppointItem(item) {
			let data = {
				approve: 'ok',
				...item
			};
			const res = await this.$myRequest({
				url: '/api/v1/ams/productionRequirementChange',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					icon: 'success',
					duration: 2000
				});
				setTimeout(() => {
					this.getOrderSection();
				}, 2200);
			} else {
				uni.showToast({
					title: res.data.message,
					icon: 'error',
					duration: 2000
				});
			}
		},
		unDeclare(item) {
			uni.showModal({
				cancelText: '取消',
				confirmText: '确定',
				title: '提示',
				content: '是否撤回该需求（审批后的需求无法撤回）',
				success: (res) => {
					if (res.confirm) {
						this.unDeclareInwardAppointItem(item);
					} else {
						uni.showToast({
							title: '用户取消操作',
							type: 'info',
							duration: 2000
						});
					}
				}
			});
		},
		async unDeclareInwardAppointItem(item) {
			let data = {
				withdraw: 'no',
				...item
			};
			const res = await this.$myRequest({
				url: '/api/v1/ams/productionRequirementChange',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					icon: 'success',
					duration: 2000
				});
				setTimeout(() => {
					this.getOrderSection();
				}, 2200);
			} else {
				uni.showToast({
					title: res.data.message,
					icon: 'error',
					duration: 2000
				});
			}
		},
		transOrderDetails(item) {
			uni.navigateTo({
				url:
					'/pages/transOrder/inwardTransOrder?params=' +
					JSON.stringify({
						requirementId: item.transRequirementId
					})
			});
		},
		//vuex
		...mapMutations('inward', ['updateInwardAppointItem'])
	}
};
</script>

<style lang="scss" scoped>
.inwardAppoint {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	&_top {
		background-color: #fff;
	}
	&_main {
		.dilCard {
			margin-top: 5rpx;
		}
	}
	&_floot {
	}
	.pop {
		&_content {
			width: 90vw;
			height: 100%;
			background-color: #fff;
		}
	}
}
</style>
